--
-- @Author:      
-- @DateTime:    2019-04-19 14:02:03
-- @Description: 用户对象管理
--  

local table_insert = table.insert

local M = class("UserManager")


-- 初始化
function M:ctor()
    self.user_list = {}
    self.seat_list = {}

    self.cur_sit_userid = nil --当前正在分配床位的玩家id
end

-- 重置数据
function M:reset()
    self.user_list = {}
    self.seat_list = {}
end

--添加用户信息
function M:addUser(user_id, user_data)
    if not user_id then 
        g.log:error("addUser游戏服务添加user时发生错误, 无效的uid ", user_id)
        return
    end
    local user = self.user_list[user_id]
	if user then	
		return user
    end
    if g.user_file_name then
        user = new(g.user_file_name, user_data)
    else
        user = new("games.common.user", user_data)
    end
    self.user_list[user_id] = user
    --通知房间管理
    self:updateDeskInfo()
	return user
end

--移除用户信息
function M:removeUser(user_id)
	local user = self.user_list[user_id]
    if not user then
		g.log:info("removeUser not exist:", g.tid, user_id)
		return false
    end 	
    self:standUp(user_id)
    self.user_list[user_id] = nil
    g.log:info("removeUser ok:", g.tid, user_id )
    g.sendGames("user_leave", g.tid, user_id, g.rid)
    self:updateDeskInfo()
	return true
end

function M:removeUsers()
    local users = self:getUsers()
    for uid, _ in pairs(users) do
        self:removeUser(uid)
    end
end

function M:setAllUserValue(name, value)
    for _, user in pairs(self.user_list) do
        user[name] = value
    end
end

function M:setUserValue(user_id, name, value)
    local user = self.user_list[user_id]
    if user then 
        user[name] = value
    end
end

-- 获取用户列表
function M:getUserList()
    return self.user_list
end

-- 获取座位用户列表
function M:getSeatList()
    return self.seat_list
end

--取玩家信息
function M:getUser(uid)
    if not uid then
        return
    end
    return self.user_list[uid]
end

function M:getUsers()
    return self.user_list
end

function M:getUsersNum()
    return table.size(self.user_list)
end

--取玩家信息
function M:getSeatUser(seat_id)
    return self.seat_list[seat_id]
end

--正在玩游戏的用户
function M:getPlayingUser()
    local users = {}
    for user_id, user in pairs(self.user_list) do 
        if user:isSeating() and user:isGaming() then
            users[user_id] = user
        end
    end
    return users
end

function M:getPlayingUserNum()
    local count = 0
    for _, user in pairs(self.user_list) do
        if user:isGaming() then
            count = count + 1
        end
    end
    return count
end

--取所有有座位的玩家
function M:getSeatUsers()
    local users = {}
    for seat_id, user in pairs(self.seat_list) do
        users[seat_id] = user
    end
    return users
end

function M:getSeatUsersNum()
    local num = 0
    for _, _ in pairs(self.seat_list) do
        num = num + 1
    end
    return num
end

function M:getSeatPlayerNum()
    local num = 0
    for _, user in pairs(self.seat_list) do
        if user:isPlayer() then
            num = num + 1
        end
    end
    return num
end

--取玩别的玩家
function M:getOthers(user_id)
    local users = {}
    for k, v in pairs(self.user_list) do
        if v:getUserId() ~= user_id then
            table_insert(users, v)
        end
    end
    return users
end

--玩家人数
function M:getPlayerNum()
    local num = 0
    for k, v in pairs(self.user_list) do
        if v:isPlayer() then
            num = num + 1
        end
    end
    return num
end

--机器人数
function M:getRobotNum()
    local num = 0
    for _, user in pairs(self.user_list) do
        if user:isRobot() then
            num = num + 1
        end
    end
    return num
end


--坐下
function M:sitDown(user_id, seat_id)
    ::SITDWON::
    self.cur_sit_userid = user_id

    local cfg = g.room_cfg
    local seats = {}
    if seat_id and seat_id>0 then --指定座位
        local user = self.seat_list[seat_id]
        if not user or user==0 then
            table_insert(seats, seat_id)
        else
            print("sitDown座位上已经有人 ",user_id, seat_id, self.seat_list)
            return false
        end
    else
        --找到空座位
        for id = 1, cfg.seat_count do
            local user = self.seat_list[id]
            if not user or user == 0 then
                table_insert(seats, id)
            end
        end
    end
    table.mix(seats) --随机位置    
    if #seats > 0 then
        seat_id = seats[1]
    else
        print("没有座位了-------")
        return false
    end
    local user = self.user_list[user_id]
    if not user then
        return false
    end
    if self.cur_sit_userid ~= user_id then
        --坐下的玩家发生了改变重新查找座位
        g.log:deskWarn("sitdown cur_sit_userid ~= user_id", self.cur_sit_userid, user_id)
        goto SITDWON
    end
    if user.seat_id and user.seat_id > 0 then --玩家已经坐下了，要换位置
        self:standUp(user_id)
    end
    self.seat_list[seat_id] = user
    user:setSeatId(seat_id)
    self.cur_sit_userid = nil
    self:updateDeskInfo()

    return seat_id
end

--离座
function M:standUp(user_id)
    if not user_id or user_id <=0 then
        return false
    end 
    local user = self.user_list[user_id]
    if not user then
        return false
    end
    local seatid = user:getSeatId()
    if seatid then 
        self.seat_list[seatid] = nil
        self:updateDeskInfo()
    end
    user:setSeatId(0)
    return true
end

--取可操作人数
function M:getOperPlayerNum()
    local count = 0
    local users = self:getSeatUsers()
    for _, user in pairs(users) do
        if user:isGaming() and user:getAction() ~= user.s_bet_allin then
            count = count + 1
        end
    end
    return count
end

--桌子自信， 人数
function M:updateDeskInfo()
    local user_num = 0
    local seat_num = 0
    local player_num = 0
    local robot_num = 0
    local player_seat_num = 0
    for _, v in pairs(self.user_list) do
        user_num = user_num + 1
        if v:isPlayer() then
            player_num = player_num + 1
        else
            robot_num = robot_num + 1
        end
        if v:isSeating() then
            seat_num = seat_num + 1
            if v:isPlayer() then
                player_seat_num = player_seat_num + 1
            end
        end
    end
    local data = {user_num=user_num,
                  seat_num = seat_num,
                  player_num = player_num,
                  robot_num = robot_num,
                  player_seat_num = player_seat_num,
    }
    g.sendGames("update_desk", g.tid, data )
end

return M

